package com.hdcircles.autoform.presenter;

import com.hdcircles.autoform.app.Constants;
import com.hdcircles.autoform.base.RxPresenter;
import com.hdcircles.autoform.model.bean.ChooseQuestionBean;
import com.hdcircles.autoform.model.bean.FormBean;
import com.hdcircles.autoform.model.bean.QuestionBean;
import com.hdcircles.autoform.model.bean.User;
import com.hdcircles.autoform.model.http.HttpResponse;
import com.hdcircles.autoform.model.http.RetrofitHelper;
import com.hdcircles.autoform.presenter.contract.UserListContract;
import com.hdcircles.autoform.util.LogUtil;
import com.hdcircles.autoform.util.RxUtil;
import com.hdcircles.autoform.util.SharedPreferenceUtil;

import java.util.List;

import javax.inject.Inject;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by hdcircles on 2016/11/18.
 */

public class UserListPresenter extends RxPresenter<UserListContract.View> implements UserListContract.Presenter {

    private RetrofitHelper retrofitHelper;


    @Inject
    public UserListPresenter(RetrofitHelper retrofitHelper) {
        this.retrofitHelper = retrofitHelper;
    }

    @Override
    public void getUsers() {
        Subscription subscription = retrofitHelper.getAllUsers()
                .compose(RxUtil.<HttpResponse<List<User>>>rxScheduleHelper())
                .compose(RxUtil.<List<User>>handleResult())
                .subscribe(new Action1<List<User>>() {
                    @Override
                    public void call(List<User> userList) {
                        mView.ShowUserList(userList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void getQuestion(String formId) {
        Subscription subscription = retrofitHelper.getQuestionByFormId(formId)
                .compose(RxUtil.<HttpResponse<List<QuestionBean>>>rxScheduleHelper())
                .compose(RxUtil.<List<QuestionBean>>handleResult())
                .subscribe(new Action1<List<QuestionBean>>() {
                    @Override
                    public void call(List<QuestionBean> questionBeanList) {
                        mView.getQuestion(questionBeanList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtil.d(throwable.getMessage());
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void getChoose(final QuestionBean question) {
        Subscription subscription = retrofitHelper.getChooseBean(question.getId())
                .compose(RxUtil.<HttpResponse<List<ChooseQuestionBean>>>rxScheduleHelper())
                .compose(RxUtil.<List<ChooseQuestionBean>>handleResult())
                .subscribe(new Action1<List<ChooseQuestionBean>>() {
                    @Override
                    public void call(List<ChooseQuestionBean> chooseQuestionBeen) {
                        question.setChooseQuestion(chooseQuestionBeen);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void send2User(FormBean formBean, final int index) {
        Subscription subscription = retrofitHelper.saveForm(formBean)
                .compose(RxUtil.<HttpResponse<String>>rxScheduleHelper())
                .compose(RxUtil.<String>handleResult())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        mView.sendSuccess("已发送", index);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void updateInitForm(String formId, int shared) {
        FormBean params = new FormBean();
        params.setCreator(SharedPreferenceUtil.getUserEmail());
        params.setStatus(Constants.STATUS_INITIALIZATION);
        params.setId(formId);
        params.setShared(++shared);
        Subscription subscription = retrofitHelper.updateForm(params)
                .compose(RxUtil.<HttpResponse<String>>rxScheduleHelper())
                .compose(RxUtil.<String>handleResult())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        mView.updateSuccess("已发送");
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError(throwable.getMessage());
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void shareForm(String formId, List<String> userEmailList) {
        Subscription subscription = retrofitHelper.shareForm(formId, userEmailList)
                .compose(RxUtil.<HttpResponse<String>>rxScheduleHelper())
                .compose(RxUtil.<String>handleResult())
                .subscribe(new Action1<String>() {
                               @Override
                               public void call(String s) {
                                    mView.shareSuccess(s);
                               }
                           },new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                    mView.showError(throwable.getMessage());
                            }
                        });
        addSubscribe(subscription);
    }


}
